﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using MedReg.Classes;

namespace MedReg.Utils
{
    internal class FixReport
    {
        public XmlNodeEventHandler xdoc_UnknownNode { get; set; }

        public XmlAttributeEventHandler serializer_UnknownAttribute { get; set; }

        public void Fix(string fn)
        {
            XDocument xmlDoc = XDocument.Load(fn);
            XNamespace ns = "http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition";
 //           var x = xmlDoc.Descendants(ns + "Body").Descendants(ns+ "ReportItems").Elements(ns + "Textbox").Distinct();

            
        
            var textboxes = from textbox in xmlDoc.Descendants(ns + "Body").Descendants(ns + "ReportItems").Elements(ns + "Textbox")
                               select new 
                               {
                                   Top = Double.Parse((textbox.Element(ns + "Top").Value.ToString().Replace("cm", ""))),
                                   Left = textbox.Element(ns + "Left").Value,
                                   Width = textbox.Element(ns + "Width").Value,
                                   Heigth = textbox.Element(ns + "Height").Value
                               };
                foreach (var textbox in textboxes)
                {
                    Logger.WriteLine(textbox.Top + " " + textbox.Left);
                    Logger.WriteLine(textbox.Width + " " + textbox.Heigth);
                    
                }
        }


        public void LoadReports(string startdir)
        {
            Logger.WriteLine("Сканирую директорию ...");
            try
            {


                var files = from file in Directory.EnumerateFiles(startdir,
                                                                  "*.rdlc", SearchOption.AllDirectories)
                            from line in File.ReadLines(file)
                            where line.Contains("<Value>=Parameters")
                            select new { File = file, Line = line };

                foreach (var f in files)
                {

                    Logger.WriteLine("Обрабатка ...");
                    Console.WriteLine(f.File.ToString());
                    Console.WriteLine(f.Line);
                    

                }
                Logger.WriteLine("DONE!");
                
                /*
                var files = from file in Directory.EnumerateFiles(startdir,
                            "*.rdlc", SearchOption.AllDirectories)
                            select new
                            {
                                File = file

                            };


                foreach (var f in files)
                {
                
                    Logger.WriteLine("Обрабатка ...");
                    Logger.WriteLine(f.File.ToString());
                    Fix(f.File.ToString());

                }
                Logger.WriteLine("DONE!");
                */
            }
            catch (UnauthorizedAccessException UAEx)
            {
                Logger.WriteLine(UAEx.Message);
            }
            catch (PathTooLongException PathEx)
            {
                Logger.WriteLine(PathEx.Message);
            }
        }

    }
}